﻿using Bee.Domain;
using Bee.Infrastructure;
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Linq;

namespace Bee.Repository
{
    public class LinkCrawlerServer : ILinkCrawlerInterface
    {
        private Queue<string> que = new Queue<string>();
        public List<LinkModel> GetLinks(string url)
        {
            if (que.Count >= 10)
                return new List<LinkModel>();
            que.Enqueue(url);
            List<LinkModel> list = new List<LinkModel>();
            string html = HttpHelper.DownloadHtml(url);
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(html);
            HtmlNodeCollection nodes = document.DocumentNode.SelectNodes("//*[@class='pagination']/li/a");
            foreach (var node in nodes)
            {
                if (node.Attributes["href"] != null)
                {
                    LinkModel model = new LinkModel();
                    model.Number = list.Count + 1;
                    model.URL = node.Attributes["href"].Value;
                    list.Add(model);
                    if (!que.Contains(model.URL))
                    {
                        list.AddRange(GetLinks(model.URL));
                        return list;
                    }
                }
            }
            list = list.Distinct().ToList();
            return list;
        }
    }
}
